package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.commonutils.Result;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.LoginInfoVO;
import com.atguigu.educenter.entity.vo.LoginVO;
import com.atguigu.educenter.entity.vo.RegisterVO;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exception.EduException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author zr
 * @since 2021-11-22
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 会员登录
     *
     * @param loginVO
     * @return
     */
    @Override
    public String login(LoginVO loginVO) {
        // 获取手机号跟密码
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        // 判断输入的手机号和密码是否为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new EduException("登录失败", 2001);
        }
        // 根据手机号到数据库进行查询
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(queryWrapper);
        // 判断手机号
        if (ucenterMember == null) {
            throw new EduException("登录失败", 2001);
        }
        // 判断加密后的密码跟数据库是否一致
        if (!Objects.equals(MD5.encrypt(password), ucenterMember.getPassword())) {
            throw new EduException("登录失败", 2001);
        }
        // 判断是否被禁用
        if (ucenterMember.getIsDisabled()) {
            throw new EduException("登录失败", 2001);
        }
        // 登录成功 返回token
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return token;
    }

    /**
     * 注册方法
     *
     * @param registerVO
     */
    @Override
    public void register(RegisterVO registerVO) {
        // 获取属性
        String code = registerVO.getCode();
        String nickname = registerVO.getNickname();
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        // 对输入的数据进行判空
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new EduException("输入的数据为空", 20001);
        }
        // 从redis中取出验证码并判断
        String redisCode = stringRedisTemplate.opsForValue().get(mobile);
        // 判断验证码是否正确
        if (!Objects.equals(redisCode, code)) {
            throw new EduException("您输入的验证码不正确", 20001);
        }
        // 判断手机号是否重复
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new EduException("手机号重复", 20001);
        }
        // 进行注册
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        baseMapper.insert(member);
    }

    /**
     * 根据id查询用户信息
     *
     * @param memberId
     * @return
     */
    @Override
    public LoginInfoVO getLoginInfo(String memberId) {
        UcenterMember member = baseMapper.selectById(memberId);
        LoginInfoVO loginInfoVO = new LoginInfoVO();
        BeanUtils.copyProperties(member, loginInfoVO);
        return loginInfoVO;
    }

    @Override
    public UcenterMember getOpenId(String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }


}
